﻿using System;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Collections;

namespace Texpro
{
    public partial class FMAD202 : Form
    {

        public FMAD202()
        {
            InitializeComponent();
        }

        private void FMAD202_Load(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            lbl_form_title.Text = this.Text;
            load_form();
            UseWaitCursor = false;
        }

        private void clear_fields()
        {
            dgv_detail.Rows.Clear();
        }

        private void fill_form_data()
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;
            //           
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            //
            strQuery = String.Empty;
            strQuery += " SELECT ";
            strQuery += " [design_id],";
            strQuery += " [design_name],";
            strQuery += " [size_id],";
            strQuery += " [size_name],";
            strQuery += " [unit_id],";
            strQuery += " [unit_name],";
            strQuery += " [wage_gross],";
            strQuery += " [wage_nett]";
            strQuery += " FROM [vw_weaving_wage]";
            strQuery += " ORDER BY [design_id],[size_id],[unit_id]";
            //
            sqlComm = new OleDbCommand();
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            dgv_detail.Rows.Clear();
            int r = -1;
            while (sqlRead.Read())
            {
                dgv_detail.Rows.Add();
                r++;
                dgv_detail["col_design_id",r].Value = sqlRead["design_id"].ToString();
                dgv_detail["col_design_name", r].Value = sqlRead["design_name"].ToString();
                dgv_detail["col_size_id", r].Value = sqlRead["size_id"].ToString();
                dgv_detail["col_size_name", r].Value = sqlRead["size_name"].ToString();
                dgv_detail["col_unit_id", r].Value = sqlRead["unit_id"].ToString();
                dgv_detail["col_unit_name", r].Value = sqlRead["unit_name"].ToString();
                dgv_detail["col_wage_gross", r].Value = sqlRead["wage_gross"].ToString();
                dgv_detail["col_wage_nett", r].Value = sqlRead["wage_nett"].ToString();
            }
            //
            sqlRead.Close();
            sqlConn.Close();
            sqlRead.Dispose();
            sqlComm.Dispose();
            sqlConn.Dispose();
        }

        private void load_form()
        {
            clear_fields();
            fill_form_data();
        }

        private bool save_form()
        {
            try
            {
                String strQuery = String.Empty;
                ArrayList alQueries = new ArrayList();
                OleDbConnection sqlConn = new OleDbConnection();
                OleDbCommand sqlComm = new OleDbCommand();
                OleDbTransaction sqlTran;
                //
                String user_id = String.Empty;
                String user_datetime = String.Empty;
                //
                String design_id = String.Empty;
                String size_id = String.Empty;
                String unit_id = String.Empty;
                String wage_gross = String.Empty;
                String wage_nett = String.Empty;
                //
                int dgrw = 0;
                int dgrc = 0;
                dgrc = dgv_detail.Rows.Count;
                //
                user_id = "'" + PCPUB.m_user_id + "'";
                user_datetime = "'" + PCPUB.m_get_sql_datetime() + "'";
                //
                strQuery = String.Empty;
                strQuery += "DELETE FROM [tis_weaving_wage];";
                alQueries.Add(strQuery);
                //
                if (dgrc > 1)
                {
                    for (dgrw = 0; dgrw < dgrc - 1; dgrw++)
                    {
                        design_id = "'" + dgv_detail["col_design_id", dgrw].Value.ToString() + "'";
                        size_id = "'" + dgv_detail["col_size_id", dgrw].Value.ToString() + "'";
                        unit_id = "'" + dgv_detail["col_unit_id", dgrw].Value.ToString() + "'";
                        wage_gross =  dgv_detail["col_wage_gross", dgrw].Value.ToString();
                        wage_nett = dgv_detail["col_wage_nett", dgrw].Value.ToString();
                        //
                        if ((double.Parse(wage_gross) > 0 || double.Parse(wage_nett) > 0) && (double.Parse(wage_gross) >=  double.Parse(wage_nett)))
                        {
                            strQuery = String.Empty;
                            strQuery += "INSERT INTO [tis_weaving_wage] (";
                            strQuery += "[design_id],";
                            strQuery += "[size_id],";
                            strQuery += "[unit_id],";
                            strQuery += "[wage_gross],";
                            strQuery += "[wage_nett]";
                            strQuery += ") VALUES (";
                            strQuery += design_id + ",";
                            strQuery += size_id + ",";
                            strQuery += unit_id + ",";
                            strQuery += wage_gross + ",";
                            strQuery += wage_nett + ");";
                            alQueries.Add(strQuery);
                        }
                    }
                }
                //
                sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
                sqlConn.Open();
                sqlTran = sqlConn.BeginTransaction(); ;
                sqlComm.Connection = sqlConn;
                sqlComm.Transaction = sqlTran;
                try
                {
                    for (int i =0;i<alQueries.Count; i++)
                    {
                        sqlComm.CommandText = alQueries[i].ToString();
                        sqlComm.ExecuteNonQuery();
                    }
                    sqlTran.Commit();
                }
                catch (Exception ex)
                {
                    sqlTran.Rollback();
                    PCMSG.ShowError(ex);
                }
                sqlConn.Close();
                sqlComm.Dispose();
                sqlConn.Dispose();
                return true;
            }
            catch (Exception e)
            {
                PCMSG.Show(e.Message);
                return false;
            }
        }

        private bool validate_form()
        {
            int dgrw = 0;
            int dgrc = 0;
            String wage_gross = String.Empty;
            String wage_nett = String.Empty;
            dgrc = dgv_detail.Rows.Count;
            if (dgrc > 1)
            {
                for (dgrw = 0; dgrw < dgrc - 1; dgrw++)
                {
                    wage_gross = dgv_detail["col_wage_gross", dgrw].Value.ToString();
                    wage_nett = dgv_detail["col_wage_nett", dgrw].Value.ToString();
                    if (double.Parse(wage_gross) <= 0 && double.Parse(wage_nett) > 0)
                    {
                        PCMSG.Show("'Gross Wage' value should not be zero when 'Nett Wage' value is greater than zero.");
                        dgv_detail["col_wage_gross", dgrw].Selected = true;
                        return false;
                    }
                    else if (double.Parse(wage_gross) > 0 && double.Parse(wage_nett) <= 0)
                    {
                        PCMSG.Show("'Nett Wage' value should not be zero when 'Gross Wage' value is greater than zero.");
                        dgv_detail["col_wage_nett", dgrw].Selected = true;
                        return false;
                    }
                    else if (double.Parse(wage_gross) < double.Parse(wage_nett))
                    {
                        PCMSG.Show("'Nett Wage' value should not be greater than 'Gross Wage'.");
                        dgv_detail["col_wage_nett", dgrw].Selected = true;
                        return false;
                    }
                }
            }
            return true;
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            if (validate_form() == true)
            {
                if (save_form() == true)
                {
                    UseWaitCursor = false;
                    PCMSG.Show("Weaving Wages saved successfully.");
                    this.Close();
                    UseWaitCursor = true;
                }
            }
            UseWaitCursor = false;
        }

        private void but_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgv_detail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
           
        }

    }

}